package com.example.demo.controller;

import com.example.demo.entity.UserInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UserController {
    private static final String SESSION_KEY = "USERINFO_SESSION";

    /**
     * 存储session
     * @param request
     */
    @RequestMapping("/setsess")
    public void setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute(SESSION_KEY,"zhangsan");
    }

    /**
     * 获取session
     */
    @RequestMapping("/getsess")
    public Object getSession(@SessionAttribute(SESSION_KEY) String name) {
        return "session-> " + name;
    }

    /**
     * 获取header 中的基本信息
     * @return
     */
    @RequestMapping("/gethd")
    public Object getHeader(@RequestHeader("Host")String ua) {
        return "header-> " + ua;
    }
    @RequestMapping("/getck")
    public Object getCk(@CookieValue(required = false,value = "java") String java) {
        return java;
    }

    @RequestMapping("/req")
    public Object req(UserInfo userInfo) {
        System.out.println(userInfo);
        return userInfo;
    }
    @RequestMapping("/req2")
    public Object req2(@RequestParam("username") String name,String password) {
        return "name : " + name + "|" + "password : " + password;
    }
    // 以json格式传入的对象的获取
    @RequestMapping("/req4")
    public Object req3(@RequestBody UserInfo userInfo) {
        return userInfo;
    }
    // path (url) = /user/123 优点搜索抓取关键字权重更高
    // /user?id=123
    // 从基础的url里面获取参数
    @RequestMapping("/req4/{name}/{pwd}") // 对位置敏感
    public Object req4(@PathVariable String name, @PathVariable("pwd") String password) {
        return "name->" + name + "password->" + password;
    }

    @RequestMapping("/myupload")
    public Object upload(@RequestPart("myimg")MultipartFile file) throws IOException {
        String fileName = UUID.randomUUID() +  // 文件名
                file.getOriginalFilename().substring(  // 文件后缀
                file.getOriginalFilename().lastIndexOf("."));
        File saveFile = new File("D:\\img\\" + fileName);
        file.transferTo(saveFile);
        return true;
    }

    @RequestMapping("/sayhi")
    public String say(String name,String password) {
        return "name : " + name + "|" + "password : " + password;
    }

    @RequestMapping("/h1")
    public Object getH1() {
        return "<h1>我是h1</h1>";
    }

    @RequestMapping("/say")
    public String sayHi(String name) { //默认值为null
        return "hi" + name;
    }
    // 尽量不要去使用基本类型
    @RequestMapping("/say2")
    public String sayHi2(int id) { // id没有传递 会报错
        return "hi" + id;
    }
    @RequestMapping("/say3")
    public String sayHi3(Integer id) { // 默认值为null
        return "hi" + id;
    }

    @RequestMapping("/say4")
    public String sayHi4(HttpServletRequest request) {
        return "hi" + request.getParameter("name");
    }

    @RequestMapping("sayHello")
    public String sayHello() {
        return "hello";
    }
}
